package special;

class Allocator {
    int[] space;
    public Allocator(int n) {
        space = new int[n];
    }
    
    public int allocate(int size, int mID) {
        int n = space.length;
        int i = 0, start = -1, count = 0, ts = 0;
        while (i < n){
            if (space[i] != 0){
                ts = i + 1;
                count = 0;
            }else {
                count++;
            }
            if (count == size){
                start = ts;
                break;
            }
            i++;
        }
        if (start != - 1) {
            for (int j = start; j < start + size; j++) {
                space[j] = mID;
            }
        }
        return start;
    }
    
    public int free(int mID) {
        int n = space.length, count = 0;
        for (int i = 0; i < n; i++) {
            if (space[i] == mID){
                space[i] = 0;
                count++;
            }
        }
        return count;
    }
}